import os
import re
import json
import pandas as pd

# 设置路径
path = r"D:\BioInfo数据集\result"
output_excel = r"result_summary.xlsx"

all_results = []

def GetFilePathNameExt(filename):
    (_FilePath, _TempFilename) = os.path.split(filename)
    (_FileShortName, _FileExtension) = os.path.splitext(_TempFilename)
    return _FilePath, _FileShortName, _FileExtension

# 正则一次抓两个 JSON 块
stego_re  = re.compile(r'=== 数值保真度检查\(origin - stego file\).*?===\n({.*?})\n===', re.DOTALL)
restore_re = re.compile(r'=== 数值保真度检查\(origin - restored file\).*?===\n({.*?})\n===', re.DOTALL)

for file in os.listdir(path):
    file_path = os.path.join(path, file)
    if os.path.isfile(file_path):
        _FilePath, _FileShortName, _FileExtension = GetFilePathNameExt(file_path)
        if _FileExtension == ".txt":
            print(f"正在处理: {file}")
            readRlt = {}
            splitStrArr = _FileShortName.split('_')
            uuid = splitStrArr[0]
            readRlt['UUID'] = uuid
            sn = splitStrArr[2]
            readRlt['sn'] = sn

            # ---------- 原读取方式 ----------
            try:
                with open(file_path, "r", encoding="utf-8") as f:
                    lines = [line.rstrip("\n") for line in f.readlines()]
            except Exception as e:
                print(f"无法读取文件 {file}: {e}")
                continue

            # ---------- 原逐行逻辑 ----------
            i = 0
            while i < len(lines):
                line = lines[i].strip()
                if "基因数据 maxDiff" in line:
                    readRlt["基因数据 maxDiff"] = line.split(":")[1].rstrip(",")
                if '"pca_correlation": ' in line:
                    readRlt["pca_correlation"] = line.split(":")[1].rstrip(",")
                if '"clustering_similarity_ari": ' in line:
                    readRlt["clustering_similarity_ari"] = line.split(":")[1].rstrip(",")
                if '"is_compatible": ' in line:
                    readRlt["is_compatible"] = line.split(":")[1].rstrip(",")
                if '"聚类一致性 (ARI)": ' in line:
                    readRlt["ARI"] = line.split(":")[1].rstrip(",")
                if '"结构相似性 (近似 Mantel)": ' in line:
                    readRlt["Mantel"] = line.split(":")[1].rstrip(",")
                i += 1

            # ---------- 新增：整文本抓 JSON 块 ----------
            text = "\n".join(lines)
            stego_match = stego_re.search(text)
            if stego_match:
                try:
                    data = json.loads(stego_match.group(1))
                    for k, v in data.items():
                        readRlt[f"stego_{k}"] = v
                except Exception as e:
                    print(f"{file}  stego JSON 解析失败: {e}")

            restore_match = restore_re.search(text)
            if restore_match:
                try:
                    data = json.loads(restore_match.group(1))
                    for k, v in data.items():
                        readRlt[f"restored_{k}"] = v
                except Exception as e:
                    print(f"{file}  restored JSON 解析失败: {e}")

            all_results.append(readRlt)

# ---------- 后续处理不变 ----------
df = pd.DataFrame(all_results)
for col in df.columns:
    if df[col].dtype == object:
        df[col] = df[col].str.rstrip(',')
df.to_excel(output_excel, index=False, engine='openpyxl')